package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addContacts(Long userId, Long friendId) {
        // 1.你加对方
        // 1-1 查询你是否有对方好友信息
        Query myQuery = new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        Friend myFriend = mongoTemplate.findOne(myQuery, Friend.class);
        // 1-2判断
        if (myFriend == null) {
            // 添加好友
            myFriend = new Friend();
            myFriend.setUserId(userId);
            myFriend.setFriendId(friendId);
            myFriend.setCreated(System.currentTimeMillis());
            // 保存到mongo
            mongoTemplate.save(myFriend);

            // 先查询好友的个人动态表
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
            // 遍历并添加到自己的好友动态表
            if (CollectionUtil.isNotEmpty(myMovementList)) {
                for (MyMovement myMovement : myMovementList) {
                    // 创建好友动态数据
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setPublishId(myMovement.getPublishId()); // 好友的发布id
                    friendMovement.setCreated(myMovement.getCreated()); // 好友的发布时间
                    // 存储到自己的好友动态表中
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);
                }
            }
        }
        // 2.对方加你
        // 2-1  查询对方是否有你的好友信息
        Query friendQuery = new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        Friend duifangFriend = mongoTemplate.findOne(friendQuery, Friend.class);
        // 2-2 判断
        if (duifangFriend == null) {
            // 对方加你称为好友
            duifangFriend = new Friend();
            duifangFriend.setUserId(friendId);
            duifangFriend.setFriendId(userId);
            duifangFriend.setCreated(System.currentTimeMillis());
            // 保存
            mongoTemplate.save(duifangFriend);

            // 查询自己的个人动态表
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
            // 遍历并添加到对方的好友动态表中
            if (CollectionUtil.isNotEmpty(myMovementList)) {
                for (MyMovement myMovement : myMovementList) {
                    // 创建对方的好友动态数据
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setPublishId(myMovement.getPublishId()); // 你的的发布id
                    friendMovement.setCreated(myMovement.getCreated()); // 你的的发布时间
                    // 保存
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + friendId);
                }
            }

        }
    }

    // 查询好友
    @Override
    public PageBeanVo findContacts(Long userId, Integer pageNum, Integer pageSize) {
        // 1.构建查询对象
        Query query = new Query(Criteria.where("userId").is(userId)).skip((pageNum - 1) * pageSize).limit(pageSize);

        // 2.查询list
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);

        // 3.查询总记录数
        long count = mongoTemplate.count(query, Friend.class);

        // 4.返回分页对象
        return new PageBeanVo(pageNum, pageSize, count, friendList);
    }

    // 删除联系人
    @Override
    public void removeContacts(Long userId, Long friendId) {
        // 1.你删他
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        // 1-1 查询我的动态 获取id
        List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
        // 1-2 删除推荐给他的动态
        if (CollectionUtil.isNotEmpty(myMovementList)) {
            for (MyMovement myMovement : myMovementList) {
                mongoTemplate.remove(Query.query(Criteria.where("publishId").is(myMovement.getPublishId())), ConstantUtil.MOVEMENT_FRIEND + friendId);
            }
        }

        // 2.他删你
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId)), Friend.class);
        // 2-1 查询他的动态 获取id
        List<MyMovement> frinedMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
        // 2-2 删除推荐给你的动态
        if (CollectionUtil.isNotEmpty(frinedMovementList)) {
            for (MyMovement myMovement : frinedMovementList) {
                mongoTemplate.remove(Query.query(Criteria.where("publishId").is(myMovement.getPublishId())), ConstantUtil.MOVEMENT_FRIEND + userId);
            }
        }
    }
}
